<?php
/**
 * 体检项目管理
 *
 * User: kaikaic
 * Date: 2018/10/24
 * Time: 17:09
 */

namespace app\exam\controller;

use app\exam\model\ItemModel;
use app\exam\service\MecItemService;
use app\exam\service\MecService;
use app\exam\service\MecSetService;
use controller\BasicAdmin;
use service\ConfigService;
use think\Db;

class Item extends BasicAdmin {

    protected $table = "ExamItem";

    /**
     * 体检项目-列表
     */
    public function index() {
        $this->title = "体检项目";

        $name = $this->request->get("name");
        $mecID = intval($this->request->param("mec_id"));
        $itemSetID = intval($this->request->param("item_set_id"));

        $query = array();
        if ($mecID) {
            $query['mec_id'] = $mecID;
        }
        if ($itemSetID) {
            $query['item_set_id'] = $itemSetID;
        }
        if ($name) {
            $query['name'] = array('like', "%{$name}%");
        }
        $query['status'] = 1;
        parent::listRecordByPage($query, "", "id desc");
    }
    public function import() {
        if ($this->request->isGet()) {
            return view('import', [
                'title'=>'导入医院体检项目'
            ]);
        }

        if ($this->request->isPost()) {
            if ($this->doImportAction()) {
                $this->success('导入完成！', "");
            }else {
                $this->error('导入失败，请稍候再试！');
            }
        }
    }
    private function doImportAction() {
        $data = $this->request->post();
        $excelFile = $data['item_excel'];
        if (!$excelFile) {
            return false;
        }

        $ItemModel = new ItemModel();
        $ItemModel->readExcelFile($excelFile);

        return true;
    }
    /**
     * 体检项目-添加
     */
    public function add() {
        if ($this->request->isGet()) {
            $vo = array();
            return view('form', [
                'title'=>'新增体检项目',
                'vo'=>$vo,
                'mec_list'=>MecService::getInstance()->listMec(),
                'item_set_list'=>json_encode(MecItemService::getInstance()->listUnderMec())
            ]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->doInsertAction($data)) {
                $this->success('添加成功！', '');
            }else {
                $this->error('添加失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检项目-编辑
     */
    public function edit() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $item = MecItemService::getInstance()->find($id, 'id', 1);
            if (empty($item)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', [
                'title'=>'编辑体检项目',
                'vo'=>$item,
                'mec_list'=>MecService::getInstance()->listMec(),
                'item_set_list'=>json_encode(MecItemService::getInstance()->listUnderMec())
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertAction($data)) {
                $this->success('编辑成功！', '');
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检项目-删除
     */
    public function remove() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $item = MecItemService::getInstance()->find($id, 'id', 1);
        if (empty($item)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = MecItemService::getInstance()->update(array('status'=>2), $id, 'id', 1);
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data) {
        if (!$data['mec_id'] || !$data['item_set_id']) {
            return false;
        }

//        $data['price'] = intval($data['price']);
//        if (!$data['price']) {
//            return false;
//        }

        if (empty($data['id'])) {
            $data['sort'] = MecItemService::getInstance()->getMaxSort(1) + 1;
            $data['status'] = 1;
            $data['create_time'] = time();

            $result = MecItemService::getInstance()->insert($data, 1);
        } else {
            $id = intval($data['id']);
            $result = MecItemService::getInstance()->update($data, $id, 'id', 1);
        }

        return $result;
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);

        for ($i=0; $i<$size; $i++) {
            $mecID = $data[$i]['mec_id'];
            $itemSetID = $data[$i]['item_set_id'];

            $data[$i]['mec'] = "";
            $mec = MecService::getInstance()->find($mecID);
            if ($mec) {
                $data[$i]['mec'] = $mec['name'];
            }

            $data[$i]['item_set'] = "";
            $itemSet = MecItemService::getInstance()->single(array('id'=>$itemSetID));
            if ($itemSet) {
                $data[$i]['item_set'] = $itemSet['name'];
            }
        }

        $this->assign("mec_list", MecService::getInstance()->listMec());
        $this->assign("item_set_list", json_encode(MecItemService::getInstance()->listUnderMec()));
    }
}

